package primetest.dto;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

public class TreatmentRecordDto {
	private int seq;
	private PatientDto patientDto;
	private DoctorDto doctorDto;
	private DoctorDto giveMedicineDoctorDto;
	private int patientSeq = -1;
	private int doctorSeq = -1;
	private int departmentID;
	private String medcineIDsAndNums;
	private List<Medicine> medicines;
	private float fee;
	private String doctorComment;
	private String symptom;
	private Date treatDate;
	private int status;

	public static final int EDIT_STATUS = 1;
	public static final int SAVE_STATUS = 2;
	public static final int PAID_STATUS = 3;
	public static final int FINISH_STATUS = 4;

	public static class Medicine {
		public MedicineDto key;
		public int quantity;
		public Medicine(MedicineDto key, int quantity) {
			this.key = key;
			this.quantity = quantity;
		}
		public MedicineDto getKey() {
			return key;
		}
		public int getQuantity() {
			return quantity;
		}
		public void setQuantity(int q) {
			quantity = q;
		}
	}

	public TreatmentRecordDto(){
		super();
		medcineIDsAndNums = "";
		treatDate = new Date(System.currentTimeMillis());
		this.medicines = new ArrayList<Medicine>();
		status = EDIT_STATUS;
	}

	public TreatmentRecordDto(int seq, PatientDto patientDto, DoctorDto doctorDto, DoctorDto giveMedicineDoctorDto, int departmentID, String medcineIDsAndNums, float fee, String doctorComment, String symptom,
			Date treatDate, int status) {
		this.seq = seq;
		this.patientDto = patientDto;
		this.doctorDto = doctorDto;
		this.setGiveMedicineDoctorDto(giveMedicineDoctorDto);
		this.departmentID = departmentID;
		this.medcineIDsAndNums = medcineIDsAndNums;
		this.fee = fee;
		this.doctorComment = doctorComment;
		this.symptom = symptom;
		this.treatDate = treatDate;
		this.status = status;
		if (patientDto != null) {
			this.patientSeq = patientDto.getSeq();
		}
		if (doctorDto != null) {
			this.doctorSeq = doctorDto.getSeq();
		}
	}

	public int getSeq() {
		return seq;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}

	public String getPatientName() {
		if (patientDto != null) {
			return patientDto.getName();
		} else {
			return "";
		}
	}

	public int getPatientSeq() {
		return patientSeq;
	}

	public void setPatientSeq(int seq) {
		patientSeq = seq;
	}

	public String getDoctorName() {
		if (doctorDto != null) {
			return doctorDto.getName();
		} else {
			return "";
		}
	}

	public int getDoctorSeq() {
		return doctorSeq;
	}

	public void setDoctorSeq(int seq) {
		doctorSeq = seq;
	}

	public int getDepartmentID() {
		return departmentID;
	}

	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}

	public String getMedcineIDsAndNums() {
		return medcineIDsAndNums;
	}

	public void setMedcineIDsAndNums(String medcineIDsAndNums) {
		this.medcineIDsAndNums = medcineIDsAndNums;
	}

	public float getFee() {
		return fee;
	}

	public void setFee(float fee) {
		this.fee = fee;
	}

	public String getDoctorComment() {
		return doctorComment;
	}

	public void setDoctorComment(String doctorComment) {
		this.doctorComment = doctorComment;
	}

	public String getSymptom() {
		return symptom;
	}

	public void setSymptom(String symptom) {
		this.symptom = symptom;
	}

	public Date getTreatDate() {
		return treatDate;
	}

	public void setTreatDate(Date treatDate) {
		this.treatDate = treatDate;
	}



	public boolean isValid() {
		return true;
	}

	public void setPatientDto(PatientDto dto) {
		patientSeq = dto.getSeq();
		patientDto = dto;
	}

	public void setDoctorDto(DoctorDto dto) {
		doctorSeq = dto.getSeq();
		doctorDto = dto;
	}

	public void save() {
		genMedcineIDsAndNumsByMedicines();
	}

	public void setAll(TreatmentRecordDto dto) {
		this.seq = dto.seq;
		this.patientDto = dto.patientDto;
		this.doctorDto = dto.doctorDto;
		this.departmentID = dto.departmentID;
		this.medcineIDsAndNums = dto.medcineIDsAndNums;
		this.fee = dto.fee;
		this.doctorComment = dto.doctorComment;
		this.symptom = dto.symptom;
		this.treatDate = dto.treatDate;
		this.status = dto.status;
		this.doctorSeq = dto.doctorSeq;
		this.patientSeq = dto.patientSeq;
		this.medicines = new ArrayList<Medicine>();
		for (Medicine m : dto.medicines) {
			this.medicines.add(new Medicine(m.key, m.quantity));
		}
	}

	public PatientDto getPatientDto() {
		return patientDto;
	}

	public DoctorDto getDoctorDto() {
		return doctorDto;
	}

	private void genMedcineIDsAndNumsByMedicines() {
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (Medicine m : medicines) {
			if (first) {
				first = false;
			} else {
				sb.append(":");
			}
			sb.append(m.key.getKey() + "!" + m.quantity);
		}
		this.medcineIDsAndNums = sb.toString();
		float sum = 0;
		for (Medicine m : medicines) {
			sum += m.getQuantity() * m.getKey().getPrice();
		}
		this.fee = sum;
	}

	public List<Medicine> getMedicines() {
		return medicines;
	}

	public void setMedicines(List<Medicine> medicines) {
		this.medicines = medicines;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public boolean isCanEdit() {
		return status == EDIT_STATUS;
	}

	public boolean isCanPay() {
		return status == SAVE_STATUS;
	}

	public boolean isCanGive() {
		return status == PAID_STATUS;
	}

	public DoctorDto getGiveMedicineDoctorDto() {
		return giveMedicineDoctorDto;
	}

	public void setGiveMedicineDoctorDto(DoctorDto giveMedicineDoctorDto) {
		this.giveMedicineDoctorDto = giveMedicineDoctorDto;
	}

	public String getGiveMedicineDoctorName() {
		if (giveMedicineDoctorDto != null) {
			return giveMedicineDoctorDto.getName();
		} else {
			return "";
		}
	}

	public int getGiveMedicineDoctorSeq() {
		if (giveMedicineDoctorDto != null) {
			return giveMedicineDoctorDto.getSeq();
		} else {
			return -1;
		}
	}

	public String getStatusName() {
		switch (status) {
		case EDIT_STATUS:
			return "Edit";
		case SAVE_STATUS:
			return "Saved";
		case PAID_STATUS:
			return "Paid";
		case FINISH_STATUS:
			return "Finished";
		}
		return "";
	}
}
